package cn.com.czpb.client.module.nanning.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.ToolBarActivityWhite;
import cn.com.czpb.client.custorm.editcolumn.CityDragGridView;
import cn.com.czpb.client.model.event.CurrentCityEvent;
import cn.com.czpb.client.model.event.CurrentCityEventForMainAndZiXun;
import cn.com.czpb.client.model.news.AreaChannelBean;
import cn.com.czpb.client.model.news.AreaChannelEntity;
import cn.com.czpb.client.module.nanning.adapter.ChannelCityAdapter;
import cn.com.czpb.client.module.news.adapter.CityDragAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/11/7.
 */

public class ChangeCityActivity extends ToolBarActivityWhite {
    @BindView(R.id.changecity_recycleView)
    RecyclerView recycleCityId;
    @BindView(R.id.common_choice)
    CityDragGridView common_choice;
    @BindView(R.id.back_iv)
    ImageView backx;
    private ChannelCityAdapter adapter;
    private CityDragAdapter adaptercommon;

    @Override
    public int setMyContentView() {
        return R.layout.activity_changecity;
    }

    @Override
    public void initView() {
        setBackVisiable(false);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleCityId.setLayoutManager(manager);
        backx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void initData() {
        getChannelJson();
        common_choice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaChannelBean bean = (AreaChannelBean) adaptercommon.getItem(position);
                spu.setCurrentCity(bean.getName());
                spu.setCurrentCityId(bean.getId());
                spu.setCurrentCounty(null);
                spu.setIsLarge(bean.getIslarge());
                CurrentCityEventForMainAndZiXun event2 = new CurrentCityEventForMainAndZiXun();
                EventBus.getDefault().post(event2);
                CurrentCityEvent event = new CurrentCityEvent();
                event.setCurrentCity(bean.getName());
                event.setCurrentCityId(bean.getId());
                event.setPosition(0);
                EventBus.getDefault().post(event);
                activity.finish();

//                LogUtils.i("1city-->"+item.getName());
//                SPUtil.getInstance().setCurrentCity(item.getName());
//                SPUtil.getInstance().setCurrentCityId(item.getId());
//                SPUtil.getInstance().setCurrentCounty(item);
//                SPUtil.getInstance().setCurrentCountryPosition(position);
//                SPUtil.getInstance().setIsLarge(item.getIslarge());
//                CurrentCityEventForMainAndZiXun event2 = new CurrentCityEventForMainAndZiXun();
//                EventBus.getDefault().post(event2);
//                CurrentCityEvent event = new CurrentCityEvent();
//                event.setPosition(position);
//                LogUtils.i("2city-->"+item.getChild().get(position).getName());
//                event.setCurrentCity(item.getChild().get(position).getName());
//                EventBus.getDefault().post(event);
//                activity.finish();
            }
        });
    }
    //获取,栏目title;
    private void getChannelJson() {
        Map<String, String> map = new HashMap<>();
        map.put("siteid", InterfaceJsonfile.SITEID);
        if(spu.getCurrentCityId() != null){
            map.put("id",spu.getCurrentCityId());
        }else {
            map.put("id", "1");
        }
        Factory.provideHttpService()
                .getArealist(ParamUtils.getRequestParam(map))
                .subscribeOn(Schedulers.io())
                .filter(new Func1<AreaChannelEntity, Boolean>() {
                    @Override
                    public Boolean call(AreaChannelEntity entity) {
                        LogUtils.e("code--" + entity.code);
                        return "200".equals(entity.code);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<AreaChannelEntity>() {
                    @Override
                    public void call(AreaChannelEntity entity) {
                        adapter = new ChannelCityAdapter(entity.data.getOther(),activity);
                        recycleCityId.setAdapter(adapter);
                        adaptercommon = new CityDragAdapter(activity,entity.data.getCommon(),0);
                        common_choice.setAdapter(adaptercommon);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable-->" + throwable.toString());
                    }
                });
    }
//    @Override
//    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//        AreaChannelBean bean = (AreaChannelBean) adapter.getItem(position);
//        spu.setCurrentCity(bean.getName());
//        spu.setCurrentCounty(null);
//        CurrentCityEvent event = new CurrentCityEvent();
//        event.setCurrentCity(bean.getName());
//        EventBus.getDefault().post(event);
//        this.finish();
//    }
}
